package com.aiju.cash.ui.account;

import android.os.Bundle;
import android.view.View;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.account.adapter.AccountTypeAdapter;
import com.aiju.cash.ui.account.manager.AccountType;
import com.aiju.cash.ui.account.manager.AccountTypeManager;
import com.aiju.cash.widgit.draglistview.DragSortListView;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-30 10:22
 * @version: V1.0
 * @Description: 管理类目
 */
public class AccountTypeActivity extends BaseTitleActivity {

    private List<AccountType> accountType;
    private DragSortListView listView;

    private AccountTypeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_type);
        setCenterTitle("管理类目");

     /*   setRightTitle("排序");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAiJuTitleBarView.getRightTitle().equals("排序")) {
                    adapter.setDrag(true);
                    setRightTitle("保存");
                } else {
                    setRightTitle("排序");
                    adapter.setDrag(false);
                    AccountTypeManager.saveAccountType(accountType);
                }
            }
        });*/
        listView = (DragSortListView) findViewById(R.id.dslvList);
        listView.setDropListener(onDrop);


        findViewById(R.id.btn_type_create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AccountTypeAddActivity.class);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        accountType = AccountTypeManager.getAccountType();
        adapter = new AccountTypeAdapter(this, accountType);
        listView.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        AccountTypeManager.saveAccountType(accountType);
        super.onBackPressed();
    }

    //监听器在手机拖动停下的时候触发
    private DragSortListView.DropListener onDrop =
            new DragSortListView.DropListener() {
                @Override
                public void drop(int from, int to) {//from to 分别表示 被拖动控件原位置 和目标位置
                 /*   if (from == 0 || to == 0) {
                        return;
                    }*/
                    if (from != to) {
                        AccountType item = (AccountType) adapter.getItem(from);//得到listview的适配器
                        adapter.remove(from);//在适配器中”原位置“的数据。
                        adapter.insert(item, to);//在目标位置中插入被拖动的控件。
                    }
                }
            };
}
